/**
 * 数据统计管理初始化
 */
var TheTestInfo = {
    id: "TheTestInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TheTestInfo.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {
            field: '序号',
            title: 'Number',
            formatter: function (value, row, index) {
                return index+1;
            }
        },
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle', hidden: true},
        {title: '考试名称', field: 'theTestName', align: 'center', valign: 'middle',},
        {title: '考试密码', field: 'passWord', align: 'center', valign: 'middle',},
        {
            title: '考试时间',
            field: 'timeLength',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return value+"分钟";
            }
        },
        {
            title: '创建时间', field: 'createTime', align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '试卷总分', field: 'totalScore', align: 'center', valign: 'middle',},
        {title: '通过分数：', field: 'scoresOfAdoption', align: 'center', valign: 'middle',},
        {title: '创建人', field: 'createPeople', align: 'center', valign: 'middle'},
        {title: '备注', field: 'remark', align: 'center', valign: 'middle'},

    ];
};


/**
 * 检查是否选中
 */
TheTestInfo.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        TheTestInfo.seItem = selected[0];
        return true;
    }
};
/**
 * 删除考试
 */
TheTestInfo.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/theTest/delete/"+this.seItem.id, function (data) {
            Feng.success("删除成功!");
            TheTestInfo.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
};
/**
 * 提交添加考试
 */
TheTestInfo.openAdd = function() {
    var index = layer.open({
        type: 2,
        title: '添加考试',
        area: ['800px', '450px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/theTest/theTest_add/'
    });
    this.layerIndex = index;
}
/**
 * 提交修改考试
 */
TheTestInfo.openChange = function() {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改考试',
            area: ['800px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/theTest/theTest_edit/' + this.seItem.id
        });
        this.layerIndex = index;
    }
}
/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
TheTestInfo.formParams = function() {
    var queryData = {};
    queryData['theTestName'] = $("#theTestName").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    return queryData;
}

/**
 * 查询考试列表
 */
TheTestInfo.search = function () {
    TheTestInfo.table.refresh({query: TheTestInfo.formParams()});
};

TheTestInfo.resetSearch=function(){
    $("#theTestName").val("");
    $("#startTime").val("");
    $("#endTime").val("");
    TheTestInfo.search();
};

var parseParam = function(param, key) {
    var paramStr = "";
    if (param instanceof String || param instanceof Number || param instanceof Boolean) {
        paramStr += "&" + key + "=" + param;
    } else {
        $.each(param, function(i) {
            var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
            paramStr += '&' + parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};


$(function () {
    $(function () {
        var defaultColunms = TheTestInfo.initColumn();
        var table = new BSTable(TheTestInfo.id, "/theTest/list", defaultColunms);
        table.setPaginationType("server");
        table.setQueryParams(TheTestInfo.formParams());
        TheTestInfo.table = table.init();
        // LoanProduct.search();
    });

});
